
global data_folder_final "W:\intimate\data"
global log_folder "W:\intimate\dofiles\logs"
global result_folder "W:\intimate\results_revision"


use  "W:\intimate\data\dvonly_all.dta", clear 
bys shnro ao_shnro : egen first_report = min(year_event)
g perp = shnro 
g victim = ao_shnro 
drop *shnro
ren victim shnro 
ren perp sphnro0
keep shnro sphnro0 first_report
duplicates drop 
tempfile first_report
save `first_report'

use "W:\intimate\data\match_victim_data_clean_new", clear
keep if dv_couple==1
merge m:1 shnro sphnro0 using `first_report'
keep if _merge==3
drop _merge

g time_to_first = (first_report - year_start_cohab)
g report_early = (time_to_first <=1 )
g av_prior_inc = (tyotuB1 + tyotuB2 + tyotuB3)/3
su av_prior_inc, d
g high_earn = (av_prior_inc > r(p50))



g tmp = .
lab var tmp "Early Report"
foreach thing in college ptoim1B1 high_earn {
	replace tmp = `thing'
	lab var tmp "Early Report"
	reg report_early tmp, vce(cluster shnro) 
	est sto r`thing'
}

coefplot (rcollege, label("College") bcolor(blue)) ///
			(rptoim1B1, label("Employed") bcolor(red)) ///
			(rhigh_earn, label("Above Av. Earn") bcolor(green)), ///
			keep(tmp) recast(bar) citop ciopts(recast(rcap)) barwidth(0.2) ///
			vertical legend(rows(1)) graphregion(color(white)) ///
			ylabel(-.12(0.03)0) yline(0) 
graph export "$result_folder\victim_het_time_to_report.pdf", replace

cap drop tmp 
g tmp = .
lab var tmp "Early Report"
xtile earn_group = av_prior_inc, nq(5)
forvalues i=1/5{
    gen earng`i'=(earn_group==`i')
}

foreach thing in earng1 earng2 earng3 earng4 earng5 {
	replace tmp = `thing'
	lab var tmp "Early Report"
	reg report_early tmp, vce(cluster shnro) 
	est sto r`thing'
}

coefplot (rearng1, label("Q1") bcolor(blue)) ///
			(rearng2, label("Q2") bcolor(red)) ///
			(rearng3, label("Q3") bcolor(green)) ///
			(rearng4, label("Q4") bcolor(brown)) ///
			(rearng5, label("Q5") bcolor(purple)), ///
			keep(tmp) recast(bar) citop ciopts(recast(rcap)) barwidth(0.12) ///
			vertical legend(rows(1)) graphregion(color(white)) ///
			ylabel(-0.12(0.03)0.12) yline(0) 
graph export "$result_folder\victim_earn_het_time_to_report.pdf", replace

// Event studies
use "W:\intimate\data\match_victim_data_clean_new", clear


local varlist sphnro ptoim1 saiprva tyotu nchild info_missing same_spouse
g same_spouse0 = 1
forvalues i = 1/5 {
	g same_spouseB`i' = (sphnro0 == sphnroB`i')
}
forvalues i = 1/5 {
	g same_spouseF`i' = (sphnro0 == sphnroF`i')
}

forvalues i = 1/5 {
    local j = 6 -`i'
    foreach thing in `varlist' {
		ren `thing'B`i' `thing'`j'
	}
}

foreach thing in `varlist' {
    ren `thing'0 `thing'6
}

forvalues i = 1/5 {
    local j = 6 +`i'
    foreach thing in `varlist' {
		ren `thing'F`i' `thing'`j'
	}
}

g group_id = _n



reshape long `varlist', i(group_id) j(time)

*Define dummies used in the event studies
drop year year_event
g time_ = time-6
egen time_year_cohab = group(time_ year_start_cohab)
g year = year_start_cohab + (time - 6)
gen treat= dv_couple==1

*Time displacement dummies
g dpl_5=time_==-5 & treat==1
g dpl_4=time_==-4 & treat==1
g dpl_3=time_==-3 & treat==1
g dpl_2=time_==-2 & treat==1
g dpl_1=time_==-1 & treat==1
g dpl_0=time_==0 & treat==1
gen dpl1=time_==1 & treat==1
gen dpl2=time_==2 & treat==1
gen dpl3=time_==3 & treat==1
gen dpl4=time_==4 & treat==1
gen dpl5=time_==5 & treat==1

gen treatPost= treat==1 & time_>=0
egen full_fe = group(match_id time)
global dummies =  "dpl_5 dpl_4 dpl_3 dpl_2 dpl_1 dpl_0 dpl1 dpl2 dpl3 dpl4 dpl5"
global fe = "full_fe year_start_cohab" 
global cluster = "match_id"



/* Heterogeneity by Time to Report */
preserve
use "W:\intimate\data\dvonly_all.dta", clear

merge m:1 shnro ao_shnro using "W:\intimate\data\dv_couple_ids"
keep if _merge==3
drop _merge

bys shnro ao_shnro : egen first_dv = min(year_event)
g time_diff = first_dv - year_start_cohab		

g late = (time_diff > 1)
ren shnro sphnro
ren ao_shnro shnro

keep shnro sphnro year_start_cohab first_dv late
duplicates drop 
tempfile time_file
save `time_file'

restore

merge m:1 shnro sphnro year_start_cohab using `time_file'
drop if _merge==2
drop _merge 

replace late = 0 if missing(late)

bys match_id : egen late_match = max(late)


capture program drop eventStudyGraphs_victim
program define eventStudyGraphs_victim
	args a b c d e
	
	quietly {
	
	preserve
	gen t = _n
	replace t = t-11 
	replace t = . if t > 5
	
	gen coef_est =. 
	gen se_est = . 

	
	
	noisily:  reghdfe `a' $dummies if `d'==`e', absorb($fe)  cluster($cluster)	
	
	
	*Store coef_estficients
	forvalues i= 0(1)5 {
		cap	replace coef_est= _b[dpl_`i']  if t == -`i'
		cap	replace se_est =  _se[dpl_`i']  if t == -`i'
	}
		
	forvalues i= 1(1)5 {
		cap	replace coef_est = _b[dpl`i']  if t == `i'
		cap	replace se_est =  _se[dpl`i']  if t == `i'
	}

	*replace coef_est = 0 if t == - 1
	*replace se_est = 0 if t == -1
	replace t = . if missing(coef_est)

	gen uCi = coef_est + se_est*1.96
	gen lCi  = coef_est - se_est*1.96
	
	*Main regression  
	reghdfe `a'  treatPost if `d'==`e',  absorb($fe)  cluster($cluster)
	local beta = string(_b[treatPost], "%10.3fc")
	local se = string(_se[treatPost], "%10.3fc")
	gen obs = e(N)
		
	twoway 		(rarea uCi lCi t ,color(gs10%50) lwidth(none) )  ///
		(connected coef_est t, msymbol(O)  lcolor(gs2) mcolor(gs2) lpattern(longdash_dot)  xlabel(-5 (1) 5) ylab(`b')  ///
	     yline(0, lpattern(dash) lcolor(black)) xline(0, lpattern(dash) lcolor(black)) ytitle(`c') xtitle("Time from Cohabitation") ), ///
		 graphregion(color(white)) legend(off)
	graph export "$result_folder\victim_match_timereport`e'_`a'.pdf", replace
	restore
	}
end



eventStudyGraphs_victim "ptoim1" "-0.1(0.02)0.02" "Employment" "late_match" "0"
eventStudyGraphs_victim "ptoim1" "-0.1(0.02)0.02" "Employment" "late_match" "1"

